package com.digua.tank;

import java.awt.*;
import java.util.Random;

/**
 * @author wangdigua
 * @title 坦克的封装类
 * @date 2020/11/7 11:24 上午
 * @since 1.0.0
 */
public class Tank {

    public static final int WIDTH = ResourceMgr.tankD.getWidth();
    public static final int HEIGHT = ResourceMgr.tankD.getHeight();
    private int x = 200 , y = 200;
    private Dir dir = Dir.DOWN;
    private int SPEED = 1;
    //tank 移动状态
    private boolean moving = true;
    private TankFrame tankFrame ;
    private boolean living = true;
    private Random random = new Random();
    private Group group = Group.BAD;
    public Rectangle rectangle = new Rectangle();

    public Tank(int x, int y, Dir dir,TankFrame tankFrame,Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tankFrame = tankFrame;
        this.group = group;
        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.width = WIDTH;
        rectangle.height = HEIGHT;
    }

    /**
     * 设计的概念，这段代码放在tank里面是最合适的
     * 画tank 这个tank自己是最清楚的，所以封装在tank对象里面是最好的
     *
     * @param g
     */
    public void paint(Graphics g) {
        if (!living) {
            tankFrame.tanks.remove(this);
            return;
        }
        switch (dir) {
            case LEFT:
                g.drawImage(this.group == Group.BAD ? ResourceMgr.tankL : ResourceMgr.goodTankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.BAD ? ResourceMgr.tankR : ResourceMgr.goodTankR,  x, y, null);
                break;
            case UP:
                g.drawImage(this.group == Group.BAD ? ResourceMgr.tankU : ResourceMgr.goodTankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.BAD ? ResourceMgr.tankD : ResourceMgr.goodTankD, x, y, null);
                break;
            default:
                break;
        }
        //重画的时候移动
        move();
    }

    /**
     * 开火
     */
    public void fire() {
        int bx = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        int by = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT /2 ;
        tankFrame.bullets.add(new Bullet(bx, by, this.dir,tankFrame,group));
    }

    private void move() {
        if (!moving) {
            return;
        }
        switch (dir) {
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
        //边界检测
        boundCheck();
        //随机开火
        randomFire();
        //随机换方向
        randomDir();
        rectangle.x = this.x;
        rectangle.y = this.y;
    }

    private void boundCheck() {
        if (this.x <= 0  )  x=0 ;
        if (this.y < 0) y = 0;
        if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) x = TankFrame.GAME_WIDTH - Tank.WIDTH;
        if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT) y = TankFrame.GAME_HEIGHT - Tank.HEIGHT;
    }

    private void randomDir() {
        if (group == Group.BAD && random.nextInt(100) < 2) {
            setDir(Dir.getRandomDir(random));
        }
    }

    private void randomFire() {
        if (group == Group.BAD && random.nextInt(100) < 2) {
            this.fire();
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }


    public void die() {
        this.living = false ;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public int getSPEED() {
        return SPEED;
    }

    public void setSPEED(int SPEED) {
        this.SPEED = SPEED;
    }
}
